package com.example.order.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.widget.Toast;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.BufferedSink;

public class Utils {

    public static void showToast(Context context, String msg, Boolean flag)
    {
        //1长true 0短false
        if(flag)
            Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
        else
            Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }
    //发送get请求
    public static void sendHttpRequest(String address, Callback callback)
    {
        //在这里加上服务器ip地址和端口号
//        address="http://192.168.0.132:8080/"+address;
        address="http://39.99.141.155:80/"+address;
        OkHttpClient client=new OkHttpClient();
        Request req=new Request.Builder()
                .url(address)
                .build();
        client.newCall(req).enqueue(callback);
    }
    //发送post请求
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void sendPostHttpRequest(String address, HashMap<String,String> map, Callback callback)
    {
        FormBody.Builder builder = new FormBody.Builder();
        Set<String> set = map.keySet();
        for (String key : set) {
            builder.add(key, Objects.requireNonNull(map.get(key)));
        }
        RequestBody body = builder.build();
//        address="http://192.168.0.132:8080/"+address;
        address="http://39.99.141.155:80/"+address;
        //System.out.println(address);
        OkHttpClient client=new OkHttpClient();
        Request req=new Request.Builder()
                .url(address)
                .post(body)
                .build();
        client.newCall(req).enqueue(callback);
    }
    //判断输入框是否为空
    private static boolean isNotNullString(String str)
    {
        //不为空返回true
        if(str==null)
            return false;
        if(str.trim().equals(""))
            return false;
        if(str.trim().length()==0)
            return false;
        return true;
    }
    //判断多个输入框是否为空
    public static boolean isNotNullString(String ... strings)
    {
        for (String string : strings) {
            if (!isNotNullString(string))
                return false;
        }
        return true;
    }
    //判断手机号码是否合法(不含卫星通信1349)
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean flag = false;
        String s2="^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";// 验证手机号
        if(isNotNullString(str)){
            p = Pattern.compile(s2);
            m = p.matcher(str);
            flag = m.matches();
        }
        return flag;
    }

}
